<template>
  <a-modal
    :open="open"
    width="780px"
    :title="rowData?.jhdh ? '修改任务' : '新增任务'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="任务代号" class="w-1/2">
        <a-input v-model:value="formState.missionCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="计划批号" class="w-1/2">
        <a-input v-model:value="formState.planNo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="任务名称" class="w-1/2">
        <a-input v-model:value="formState.missionName" placeholder="请输入" @blur="getPinyin" />
      </a-form-item>
      <a-form-item label="助记码" class="w-1/2">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="换乘工具" class="w-1/2">
        <SelectItem v-model:value="formState.transferCode" type="DT00285" placeholder="请选择" @change="onChangeTransfer" />
      </a-form-item>
      <a-form-item label="接收方式" class="w-1/2">
        <SelectItem v-model:value="formState.receiveModeCode" type="DT00286" placeholder="请选择" @change="onChangeReceiveMode" />
      </a-form-item>
      <a-form-item label="接收批次" class="w-1/2">
        <a-input v-model:value="formState.receiveNo" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="伤病员来源" class="w-1/2">
        <a-input v-model:value="formState.source" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="气象海况" class="w-full">
        <a-textarea v-model:value="formState.weatherCondition" :rows="3" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  missionCode: '',
  planNo: '',
  missionName: '',
  zjm: '',
  transferCode: '',
  transferName: '',
  receiveModeCode: '',
  receiveModeName: '',
  receiveNo: '',
  source: '',
  weatherCondition: ''
})
const rulesState = ref({})
const { isCompleted, resetFields, validate } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = props.rowData
    }
  }
)
const onChangeTransfer = (record, option) => {
  formState.value.transferCode = record
  formState.value.transferName = option.label
}
const onChangeReceiveMode = (record, option) => {
  formState.value.receiveModeCode = record
  formState.value.receiveModeName = option.label
}
const getPinyin = async (name) => {
  const { data, code } = await post('/common/util/getPinyin', { name: name.target.value })
  code === 0 && (formState.value.zjm = data)
}
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/war/receiveTask/doSaveWarReceiveTask', formState.value)
  if (code === 0) {
    message.success('保存成功')
    onCancel()
    emit('ok')
  } else {
    message.error(msg)
  }
}
</script>
